package com.zqx.strategy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

@Controller
public class DownLoadController {

    private static final String utf8="utf-8";

    @RequestMapping("/download")
    public void downLoadFile(HttpServletRequest request, HttpServletResponse response) throws Exception{

        File file = new File("G:\\视频\\商城上线\\031_前台_商城列表.mp4");
        response.setCharacterEncoding(utf8);
        InputStream is=null;
        OutputStream os=null;

        try{
            //分片下载
            long fSize = file.length();
            response.setContentType("application/x-download");
            String fileName = URLEncoder.encode(file.getName(), utf8);
            response.addHeader("Content-Disposition","attachment;filename="+fileName);
            response.setHeader("Accept-Range","bytes");
            response.setHeader("fSize",String.valueOf(fSize));
            response.setHeader("fName",fileName);

            //sum 标识已经读取内容
            long pos=0,last=fSize-1,sum=0;
            if (null!=request.getHeader("Range")) {
                response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
                //numRange只能传数组
                String numRange = request.getHeader("Range").replaceAll("bytes=", "");
                String[] strRange = numRange.split("-");
                if (strRange.length==2) {
                    pos = Long.parseLong(strRange[0].trim());
                    last= Long.parseLong(strRange[1].trim());
                    if (last>fSize-1) {
                        last=fSize-1;
                    }
                }else {
                    pos=Long.parseLong(numRange.replaceAll("-","").trim());
                }
            }
            //需要读取的字节数
            long rangeLength=last-pos+1;
            String contentRange = new StringBuffer("bytes ").append(pos).append("-").append(last).append("/").append(fSize).toString();
            response.setHeader("Content-Range",contentRange);
            response.setHeader("Content-Length",String.valueOf(rangeLength));

            os=new BufferedOutputStream(response.getOutputStream());
            is=new BufferedInputStream(new FileInputStream(file));
            is.skip(pos);
            byte[] buffer = new byte[1024];
            int length = 0;
            while (sum<rangeLength){

                int fileReadOff=(rangeLength-sum)<=buffer.length?(int)(rangeLength-sum):buffer.length;
                length=is.read(buffer,0,fileReadOff);
                sum = sum+length;
                os.write(buffer,0,length);
            }
            System.out.println("下载完成");
        }finally {
            if (is!=null) {
                is.close();
            }
            if (os!=null) {
                os.close();
            }
        }
    }
}
